package cn.game.entry;

import java.util.Iterator;
import java.util.List;
import java.util.Random;

import cn.game.core.CardGame;
import cn.game.core.Cards;
import cn.game.core.Room;

public class DriveTrainGame extends CardGame{
	
	
	public DriveTrainGame(){
		super();
	}
	
	public void init() {
//		super.init();
//		room = new DriveTrainRoom();// 初始化房间
//		cards = new Deck();// 一副牌
		 
		System.out.println("=======开始洗牌=========");
//		System.out.println(cards.getCards());
//		toShuffle(cards);// 洗牌
		System.out.println("=======结束洗牌=========");
//		System.out.println(cards.getCards());
		
		System.out.println("=======开始发牌=========");
//		toDeal(cards,room);// 发牌
		System.out.println("=======结束发牌=========");
		
		
		
	}
	
	/**
	 * 洗牌
	 * @param cards
	 */
	private void toShuffle(Cards cards) {
		List<Card> deck = cards.getCards();
		for(int i = 0 ; i < deck.size() ; i++){
			Random ran = new Random();
			int j = ran.nextInt(deck.size());
			int k = ran.nextInt(deck.size());
			change(j,k, deck);
		}
	}

	/**
	 * 交换List x,y 位置的值
	 * @param x
	 * @param y
	 * @param list
	 */
	public void change(int x,int y,List list){
		Object ob = list.get(x);
		list.set(x, list.get(y));
		list.set(y, ob);
	}
	
	/**
	 * 发牌
	 * @param deck
	 * @param room
	 */
	private void toDeal(Cards deck, Room room) {
		int count = 1;
		while(deck.getCards().size() > 0){
			Card card = deck.getCards().remove(0);
			for(Player player : room.getPlayers()){
				if(count == player.getPosition()){
					player.draw(card);
				}
			}
			
			count ++;
			if(count > 4){
				count = count -4;
			}
		}
		
		
	}


	public void start() {
		super.start();
		/*while(room.getPlayers().size() > 1){

			for(Iterator it = room.getPlayers().iterator() ; it.hasNext();){
				Player player = (Player) it.next();
				Card endCard = player.play();
				gameCards.add(endCard);
//				System.out.println("桌上的牌:"+gameCards);
				for(int i = 0 ; gameCards.size() > 1 && i < ( gameCards.size()-1 ) ; i++){
					Card card = gameCards.get(i);
					if(endCard.equalsIgnoreColor(card)){
						player.drawMore(gameCards.subList(i, gameCards.size()));
						gameCards.removeAll(gameCards.subList(i, gameCards.size()));
						break;
					}
				}
				
				if(player.getCards().size() <= 0){
					it.remove();
					System.out.println("失败者:"+player.getName());
				}
				
			}
		
		
		}*/
		
		
		end();
		
	}


	public void end() {
		super.end();
		
//		System.out.print("最后的赢家是:"+room.getPlayers().get(0).getName());
	}
}
